<?php
/**
 * 地址类
 */


class Address extends MyBase{
	
	private $tb_address;
	private $tb_province;
	private $tb_city;
	private $tb_area;
	
	function __construct() {
		parent::__construct('address');
		
		$this->tb_address = $this->get_tbname('address');
		$this->tb_province = $this->get_tbname('province');
		$this->tb_city = $this->get_tbname('city');
		$this->tb_area = $this->get_tbname('area');
	}
	
	/**
	 * 修改地址
	 */
	public function edit($userid, $post){
		$id = empty($post['id']) ? 0 : intval($post['id']);
		if(
			empty($post['province_id']) || empty($post['city_id']) || empty($post['area_id']) ||
			empty($post['consignee']) || empty($post['address']) || empty($post['mobile'])
		){
			return $this->rwarn('未提供收货人、区域、详细地址和手机');
		}
		if($id == 0){
			$count = $this->db->get_counts($this->tb_address, array('userid' => $userid));
			if($count >= 10){
				return $this->rwarn('最多只能有10个收货地址，不能再添加！！！');
			}
		}
		$data = array(
			'userid'		=> $userid,
			'consignee'		=> $post['consignee'],
			'province_id'	=> $post['province_id'],
			'city_id'		=> $post['city_id'],
			'area_id'		=> $post['area_id'],
			'address'		=> $post['address'],
			'mobile'		=> $post['mobile'],
			'tel'			=> $post['tel'],
			'email'			=> $post['email'],
			'zipcode'		=> $post['zipcode'],
			'besttime'		=> $post['besttime'],
		);
		if($id > 0){
			$data['updatetime'] = TIME;
			$id = $this->db->update($this->tb_address, array('id' => $id), $data);
		}else{
			$data['addtime'] = TIME;
			$id = $this->db->insert($this->tb_address, $data);
		}
		return $this->rjust($id, $id, 'id');
	}
			
	/**
	 * 删除地址
	 *
	 * @param int			$id
	 * @return int
	 */
	public function del($post){
		$iRtn = 0;
		if(!is_array($post['list'])){
			$post['list'] = array($post['list']);
		}
		$oLibrary = new Library();
		foreach($post['list'] as $lang_id){
			$librarys = $oLibrary->get_list('id', array('pid' => $lang_id), array(), 1, 1);
			if(!empty($librarys['list'])){
				continue;
			}
			if($this->db->delete($this->tb_address, array('id' => $lang_id))){
				$iRtn++;
			}
		}
		return $iRtn;
	}
	
	/**
	 * 取一条记录
	 */
	public function get_row($id, $fields = '*'){
		$select = array('id' => $id);
		return $this->db->get_row($this->tb_address, $select, $fields);
	}
	
	/**
	 * 取列表
	 */
	public function get_list($fields = '*', $select = array(), $order = array(), $page = 1, $size = 10, $offset = PAGE_OFFSET){
		$select['kept'] = '';
		if(!empty($select['name'])){
			$select['kept'] .= " AND `name` LIKE '%{$select['name']}%'";
			unset($select['name']);
		}
		if(!empty($select['mark'])){
			$select['kept'] .= " AND `mark` LIKE '%{$select['mark']}%'";
			unset($select['mark']);
		}
		return $this->db->get_list($this->tb_address, $fields, $select, $order, $page, $size, $offset);
	}
	
	public function get_all($fields = '*', $select = array(), $order = array()){
		$select['kept'] = '';
		if(!empty($select['name'])){
			$select['kept'] .= " AND `name` LIKE '%{$select['name']}%'";
			unset($select['name']);
		}
		if(!empty($select['mark'])){
			$select['kept'] .= " AND `mark` LIKE '%{$select['mark']}%'";
			unset($select['mark']);
		}
		return $this->db->get_all($this->tb_address, $fields, $select, $order);
	}
	
	/********************************************************************************
	 * 区域
	 ********************************************************************************/
	
	public function get_province_by_id($province_id){
		return $this->db->get_col($this->tb_province, array('province_id' => $province_id), 'province_name');
	}
	public function get_city_by_id($city_id){
		return $this->db->get_col($this->tb_city, array('city_id' => $city_id), 'city_name');
	}
	public function get_area_by_id($area_id){
		return $this->db->get_col($this->tb_area, array('area_id' => $area_id), 'area_name');
	}
	
	public function get_provinces($keyword = ''){
		$arr = array();
		$select['kept'] = '';
		if(!empty($keyword)){
			$select['kept'] .= " AND `province_name` LIKE '%{$keyword}%'";
		}
		$provinces = $this->db->get_all($this->tb_province, '`province_id`,`province_name`', $select, array('province_id' => 'ASC'));
		if(!empty($provinces) && !empty($provinces['list'])){
			foreach($provinces['list'] as $row){
				$arr[$row['province_id']] = $row['province_name'];
			}
		}
		return $arr;
	}

	public function get_citys($parent_id = 0, $keyword = ''){
		$arr = array();
		$select['kept'] = '';
		if($parent_id > 0){
			$select['parent_id'] = $parent_id;
		}
		if(!empty($keyword)){
			$select['kept'] .= " AND `city_name` LIKE '%{$keyword}%'";
		}
		$citys = $this->db->get_all($this->tb_city, '`city_id`,`city_name`', $select, array('city_id' => 'ASC'));
		if(!empty($citys) && !empty($citys['list'])){
			foreach($citys['list'] as $row){
				$arr[$row['city_id']] = $row['city_name'];
			}
		}
		return $arr;
	}

	public function get_areas($parent_id = 0, $keyword = ''){
		$arr = array();
		$select['kept'] = '';
		if($parent_id > 0){
			$select['parent_id'] = $parent_id;
		}
		if(!empty($keyword)){
			$select['kept'] .= " AND `area_name` LIKE '%{$keyword}%'";
		}
		$areas = $this->db->get_all($this->tb_area, '`area_id`,`area_name`', $select, array('area_id' => 'ASC'));
		if(!empty($areas) && !empty($areas['list'])){
			foreach($areas['list'] as $row){
				$arr[$row['area_id']] = $row['area_name'];
			}
		}
		return $arr;
	}
	
	/**
	 * 保存收货人信息到SESSION
	 * 
	 * @param array $post
	 */
	public function save_address($userid, $post){
		$select = array(
			'userid'		=> $userid,
			'province_id'	=> $post['province_id'],
			'city_id'		=> $post['city_id'],
			'area_id'		=> $post['area_id'],
		);
		$rs = $this->db->get_row($this->tb_address, $select, 'id');
		if(!empty($rs)){
			return $this->rwarn('相同区域收货地址已经存在');
		}
		$cs = $this->db->get_counts($this->tb_address, array('userid' => $userid));
		if($cs >= 10){
			return $this->rwarn('您最多只能保存10个常用收货地址');
		}
		return $this->edit($userid, $post);
	}
	
}
?>